/*
 * TView.cpp
 *
 *  Created on: May 16, 2017
 *      Author: wangguojian
 */

#include "TView.h"

#include "game/CGNode.h"

USING_NS_CC;

TView::TView(const AnchorLayout& ly)
	: mAnchorLayout(ly)
{
	setAnchorLayout(ly);
}

TView::~TView()
{
}

void TView::setAnchorLayout(const AnchorLayout& ly)
{
	mAnchorLayout = ly;
	// 3 for CAView, need change copy function
	m_eLayoutType = 3;

	if (m_bRunning) {
		if (m_pSuperview) {
			this->reViewAnchorLayout(m_pSuperview->m_obContentSize, true);
		}
//		else if (m_pParentCGNode) {
//			this->reViewAnchorLayout(m_pParentCGNode->m_obContentSize);
//		}
	}
}

void TView::reViewAnchorLayout(const CrossApp::DSize& contentSize, bool allowAnimation)
{
	if (m_eLayoutType != 3) {
		this->updateDraw();
		return;
	}

	DPoint point = DPointZero;
	DSize size = DSizeZero;
	const AnchorLeft left = mAnchorLayout.horizontal.getLeft();
	const AnchorRight right = mAnchorLayout.horizontal.getRight();
	const AnchorTop top = mAnchorLayout.vertical.getTop();
	const AnchorBottom bottom = mAnchorLayout.vertical.getBottom();
	const float width = mAnchorLayout.horizontal.getWidth();
	const float height = mAnchorLayout.vertical.getHeight();
	// first check left/right, then check width
	// first check top/bottom, then check height

	auto isHValid = [](const AnchorBase &anchor) -> bool {
		return anchor.getAlignmentH() != AnchorBase::AlignmentH::Invalid;

	};
	auto isVValid = [](const AnchorBase &anchor) -> bool {
		return anchor.getAlignmentV() != AnchorBase::AlignmentV::Invalid;
	};

	/******************* horizontal ************************/

	//left right
	if (isHValid(left) && isHValid(right)) {
		DRect lFrame = left.getAlignmentView()->getFrame();
		DRect rFrame = right.getAlignmentView()->getFrame();

		CCLOG("left frame size[%f,%f], point(%f,%f)",
				lFrame.size.width, lFrame.size.height,
				lFrame.origin.x, lFrame.origin.y);
		CCLOG("right frame size[%f,%f], point(%f,%f)",
						rFrame.size.width, rFrame.size.height,
						rFrame.origin.x, rFrame.origin.y);

		if (left.getAlignmentH() == AnchorBase::AlignmentH::Left) {
			point.x = lFrame.origin.x;
		} else { //AlignmentH right
			point.x = lFrame.origin.x + lFrame.size.width;
		}
		point.x += left.getMargin();

		//TODO check m_pSuperview is nullptr
		float xr = m_pSuperview->getFrameOrigin().x + m_pSuperview->getFrame().size.width;
		if (right.getAlignmentH() == AnchorBase::AlignmentH::Left) {
			xr = rFrame.origin.x;
		} else {
			xr = rFrame.origin.x + rFrame.size.width;
		}
		xr -= right.getMargin();
		size.width = xr - point.x;
	}
	// left width
	else if (isHValid(left) && width >= 0) {
		DRect lFrame = left.getAlignmentView()->getFrame();
		CCLOG("left frame size[%f,%f], point(%f,%f)",
				lFrame.size.width, lFrame.size.height,
				lFrame.origin.x, lFrame.origin.y);

		if (left.getAlignmentH() == AnchorBase::AlignmentH::Left) {
			point.x = lFrame.origin.x;
		} else { //AlignmentH right
			point.x = lFrame.origin.x + lFrame.size.width;
		}
		point.x += left.getMargin();
		size.width = width;
	}
	// right width
	else if (isHValid(right) && width >= 0) {
		DRect rFrame = right.getAlignmentView()->getFrame();

		CCLOG("right frame size[%f,%f], point(%f,%f)",
						rFrame.size.width, rFrame.size.height,
						rFrame.origin.x, rFrame.origin.y);

		//TODO check m_pSuperview is nullptr
		float xr = m_pSuperview->getFrameOrigin().x + m_pSuperview->getFrame().size.width;
		if (right.getAlignmentH() == AnchorBase::AlignmentH::Left) {
			xr = rFrame.origin.x;
		}
		else {
			xr = rFrame.origin.x + rFrame.size.width;
		}
		xr -= right.getMargin();
		point.x = xr - width;
		size.width = width;
	} else {
		CCLOG("------------- oOPS invalid horizontal anchor :(");
	}

	/******************* vertical ************************/

	//top bottom
	if (isVValid(top) && isVValid(bottom)) {
		DRect tFrame = top.getAlignmentView()->getFrame();
		DRect bFrame = bottom.getAlignmentView()->getFrame();

		CCLOG("top frame size[%f,%f], point(%f,%f)",
				tFrame.size.width, tFrame.size.height,
				tFrame.origin.x, tFrame.origin.y);
		CCLOG("bottom frame size[%f,%f], point(%f,%f)",
				bFrame.size.width, bFrame.size.height,
				bFrame.origin.x, bFrame.origin.y);

		if (top.getAlignmentV() == AnchorBase::AlignmentV::Top) {
			point.y = tFrame.origin.y;
		}else {
			point.y = tFrame.origin.y + tFrame.size.height;
		}
		point.y += top.getMargin();

		//TODO check m_pSuperview is nullptr
		float yb = m_pSuperview->getFrameOrigin().y + m_pSuperview->getFrame().size.height;
		if (bottom.getAlignmentV() == AnchorBase::AlignmentV::Top) {
			yb = bFrame.origin.y;
		} else {
			yb = bFrame.origin.y + bFrame.size.height;
		}
		yb -= bottom.getMargin();
		size.height = yb - point.y;
	}
	// top height
	else if (isVValid(top) && height >= 0) {
		DRect tFrame = top.getAlignmentView()->getFrame();

		CCLOG("top frame size[%f,%f], point(%f,%f)",
				tFrame.size.width, tFrame.size.height,
				tFrame.origin.x, tFrame.origin.y);

		if (top.getAlignmentV() == AnchorBase::AlignmentV::Top) {
			point.y = tFrame.origin.y;
		}else {
			point.y = tFrame.origin.y + tFrame.size.height;
		}
		point.y += top.getMargin();
		size.height = height;
	}
	// bottom height
	else if (isVValid(bottom) && height >= 0) {
		DRect bFrame = bottom.getAlignmentView()->getFrame();

		CCLOG("bottom frame size[%f,%f], point(%f,%f)",
				bFrame.size.width, bFrame.size.height,
				bFrame.origin.x, bFrame.origin.y);

		//TODO check m_pSuperview is nullptr
		float yb = m_pSuperview->getFrameOrigin().y + m_pSuperview->getFrame().size.height;
		if (bottom.getAlignmentV() == AnchorBase::AlignmentV::Top) {
			yb = bFrame.origin.y;
		} else {
			yb = bFrame.origin.y + bFrame.size.height;
		}
		yb -= bottom.getMargin();
		point.y = yb - height;
		size.height = height;
	}
	else {
		CCLOG("------------- oOPS invalid vertical anchor :(");
	}

	CCLOG("view size (%f, %f), point (%f, %f)",
			size.width, size.height,
			point.x, point.y);

	if (!this->initWithFrame(DRect(point, size))) {
		CCLOG("------------- oOPS change anchor to frame error :(");
	}
}
